﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TheCell;

namespace SudoCalc
{
    public class PossibilityUtils
    {
        public static void Possibility(List<Cell> cells)
        {
            foreach (Cell cell in cells)
            {
                cell.Change = false;
                if (cell.Status != CellStatus.Fixed)
                {
                    FilterPossible(cell); 
                }
            }
        }

        private static void FilterPossible(Cell cell)
        {
            FilterPossible(cell, ClassifyUtils.Lines[cell.Location.X]);
            FilterPossible(cell, ClassifyUtils.Columns[cell.Location.Y]);
            FilterPossible(cell, ClassifyUtils.Areas[cell.Location.Area]);
        }

        private static void FilterPossible(Cell cell, List<Cell> cells)
        {
            foreach (Cell c in cells)
            {
                if (c.Status != CellStatus.None)
                {
                    if (cell.Possible.Contains(c.Value))
                    {
                        cell.Possible.Remove(c.Value);
                        cell.Change = true;
                        if (cell.Possible.Count == 1)
                        {
                            cell.Status = CellStatus.Calc;
                            cell.Value = cell.Possible[0];
                            break;
                        }
                    }
                }
            }
        } 
    }
}
